package com.sx.jys.ui.frag

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.gyf.immersionbar.ImmersionBar
import com.sanxinsaas.nearstore.framework.util.click
import com.sx.jys.app.framework.BaseFragment
import com.sx.jys.app.net.bean.CheckBean
import com.sx.jys.databinding.FragCreateOrderBinding
import com.sx.jys.ui.adapter.ItemCheckAdapter
import com.sx.jys.ui.vm.CreateOrderVm

class FragCreateOrder : BaseFragment<CreateOrderVm, FragCreateOrderBinding>() {
    companion object {
        val instance = FragCreateOrder()
    }

    override fun initView(savedInstanceState: Bundle?) {
        (mDatabind.tvTitle.layoutParams as ViewGroup.MarginLayoutParams).let {
            it.topMargin = ImmersionBar.getStatusBarHeight(this)
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.vm = mViewModel
        mDatabind.coinAdapter =
            ItemCheckAdapter(requireContext(), mViewModel.checkCoinList).also { adapter ->
                adapter.setOnItemClickListener { viewId, item, position ->
                    mViewModel.checkCoinList.clear()
                    mViewModel.checkCoinList.add(item)
                    adapter.notifyDataSetChanged()
                }
            }
        mDatabind.multipleAdapter =
            ItemCheckAdapter(requireContext(), mViewModel.multipleList).also { adapter ->
                adapter.setOnItemClickListener { viewId, item, position ->
                    if ("-1".equals(item.id)) {
                        OptionsPickerBuilder(requireContext(), object : OnOptionsSelectListener {
                            override fun onOptionsSelect(
                                options1: Int,
                                options2: Int,
                                options3: Int,
                                v: View?
                            ) {
                                val list=mViewModel.multipleListLiveData.value!!
                                val newBean = CheckBean((options1+1).toString(), "${options1+1}倍")
                                if (list.contains(newBean)) {
                                    mViewModel.multipleList.add(newBean)
                                    adapter.notifyDataSetChanged()
                                } else {
                                    mViewModel.multipleListLiveData.addData(list.size-1,newBean)
                                }
                                mViewModel.multipleList.clear()
                                mViewModel.multipleList.add(newBean)
                                mViewModel.multipleListLiveData.callValue()
                            }

                        }).setTitleText("请选择下单倍数")
                            .setLabels("倍", "", "")
                            .build<String>()
                            .apply {
                                setPicker((1 until 101).toList().map { it.toString() })
                            }
                            .show()
                    } else {
                        mViewModel.multipleList.clear()
                        mViewModel.multipleList.add(item)
                        adapter.notifyDataSetChanged()
                    }
                }
            }
        mViewModel.initData()
        mViewModel.coinList()
        mDatabind.doMany.click {
            mViewModel.setCoinConfig(requireContext(),true)
        }
        mDatabind.doEmpty.click {
            mViewModel.setCoinConfig(requireContext(),false)
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.coinList()
    }
}